﻿package 
{
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.display.*;
	import FluidLayout.*;
	import flash.text.*;
	import flash.net.*;
	import mvc_model.*;
	import mvc_view.*;
	import mvc_controller.*;
	import flashx.textLayout.formats.BackgroundColor;

	public class Website extends MovieClip
	{
		private var _users:Users;		
		private var _usersView:UsersView;
		
		private var _nerdsPageView:NerdsPageView;
		private var _nerdsPageModel:NerdsPageModel;
		private var _nerdsController:NerdsController;
		
		private var _projectsPageModel:ProjectsPageModel;
		private var _projectsPageView:ProjectsPageView;
		private var _projectsController:ProjectsController;
		
		private var _controller:RegisterController;
		private var _registerView:RegisterView;
		private var _uploadFile:UploadFile;
		
		private var ABOUT_US_PAGE_TEXT:String = "Jobs For Nerds was developed by three Bachelor of Information Technology "
		+ "students at James Cook University for the Computer Science subject 'CP3003 Web Technologies'. \n\n"
		+ "Drewan OBrien : Hardcore agile design pattern guru. \n\n"
		+ "Yannink French : AZERTY extroardinaire. \n\n"
		+ "Jason Allen : Have you tried turning it on and off again? ";

		private var HOME_PAGE_TEXT:String = "Our development team offers tech work that can be tailor made "
		+ "to cater to your needs. Hire a specialist techie from Jobs For Nerds and get his services "
		+ "right away. That's right, try before you hire. We guarantee a quality product that is delivered "
		+ "within the discussed schedule and at a cost…that would NOT pinch your pocket. Who knows, the techie "
		+ "may well end up on your payroll...";

		private var home_content_box:HomeContentBox;
		private var projects_content_box:ProjectsContentBox;
		private var nerds_content_box:NerdsContentBox;
		private var about_us_content_box:AboutUsContentBox;
		private var search_content_box:SearchContentBox;
		private var register_content_box:RegisterContentBox;

		private var backGround:Background;
		private var searchIcon:SearchIcon;
		private var searchBar:SearchBar;
		private var navContainer:Sprite;
		private var navArray:Array;
		private var navItem:NavItem;
		private var invisible_click:Invisible;

		private var currentPage:String;

		// Ensures that when the website is added to stage the Website() class is called
		public function Website()
		{
			this.addEventListener(Event.ADDED_TO_STAGE,init);
		}

		public function init(e:Event):void
		{
			//Set the Scale Mode of the Stage
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;

			// Add the default background to the stage
			backGround = new Background  ;
			addChild(backGround);

			// Apply the fluid alignment to the background
			var backGroundParam = {x:0,y:0};
			new FluidObject(backGround,backGroundParam);

			// Add the search bar to the stage
			searchBar = new SearchBar  ;
			addChild(searchBar);

			// Apply the fluid alignment to the search bar
			var searchBarParam = {x:1,y:0,offsetX: -  searchBar.width - 20,offsetY:50};
			new FluidObject(searchBar,searchBarParam);

			// Add the search icon to the stage
			searchIcon = new SearchIcon  ;
			addChild(searchIcon);

			// Set the search icon to button mode and set up the click handler
			searchIcon.name = "SearchIcon";
			searchIcon.buttonMode = true;
			searchIcon.addEventListener(MouseEvent.CLICK, onClick);

			// Apply the fluid alignment to the search bar;
			var searchIconParam = {x:1,y:0,offsetX: -  searchIcon.width - 22,offsetY:52};
			new FluidObject(searchIcon,searchIconParam);

			// Add the navigation container to the stage
			navContainer = new Sprite  ;
			addChild(navContainer);

			// Create the navigation array
			navArray = ["Home","Projects","Nerds","Register","About Us"];

			// Add the buttons to the stage
			for (var i:Number = 0; i < 5; i++)
			{
				navItem = new NavItem  ;
				navItem.x = navItem.width * i;
				navItem.name = navArray[i];//String(i);
				navItem.nav_name.text = navArray[i];
				navItem.inv_click.buttonMode = true;
				navItem.nav_name.selectable = false;

				navItem.addEventListener(MouseEvent.CLICK,onClick);
				navItem.addEventListener(MouseEvent.ROLL_OVER,doRollOver);
				navItem.addEventListener(MouseEvent.ROLL_OUT,doRollOut);

				navContainer.addChild(navItem);
			}

			// Apply the fluid alignment to the navigation container
			var navMenuParam = {x:1,y:0,offsetX: -  navContainer.width - 30,offsetY:100};
			new FluidObject(navContainer,navMenuParam);

			// Add the home content box to the stage and fill the content text with lorem ipsum...
			home_content_box = new HomeContentBox();
			addChild(home_content_box);
			home_content_box.visible = false;

			home_content_box.content_txt.text = "Welcome to Jobs For Nerds\n\n" + HOME_PAGE_TEXT;

			// Apply the alignment to the home content box
			var homeContentBoxParam = {x:0,y:0,offsetX:175,offsetY:200};
			new FluidObject(home_content_box,homeContentBoxParam);

			// Add the Projects content box to the stage
			projects_content_box = new ProjectsContentBox();
			addChild(projects_content_box);
			projects_content_box.visible = false;

			// Apply the alignment to the Projects content box
			var ProjectsContentBoxParam = {x:0,y:0,offsetX:175,offsetY:200};
			new FluidObject(projects_content_box,ProjectsContentBoxParam);

			// Add the nerds content box to the stage
			nerds_content_box = new NerdsContentBox();
			addChild(nerds_content_box);
			nerds_content_box.visible = false;

			// Apply the alignment to the nerds content box
			var nerdsContentBoxParam = {x:0,y:0,offsetX:175,offsetY:200};
			new FluidObject(nerds_content_box,nerdsContentBoxParam);

			// Add the about us content box to the stage
			about_us_content_box = new AboutUsContentBox();
			addChild(about_us_content_box);
			about_us_content_box.visible = false;
			
			about_us_content_box.content_txt.text = ABOUT_US_PAGE_TEXT;

			// Apply the alignment to the about us content box
			var AboutUsContentBoxParam = {x:0,y:0,offsetX:175,offsetY:200};
			new FluidObject(about_us_content_box,AboutUsContentBoxParam);

			// Add the search content box to the stage
			search_content_box = new SearchContentBox();
			addChild(search_content_box);
			search_content_box.visible = false;

			// Apply the alignment to the search content box
			var SearchContentBoxParam = {x:0,y:0,offsetX:175,offsetY:200};
			new FluidObject(search_content_box,SearchContentBoxParam);

			// Add the register content box to the stage
			register_content_box = new RegisterContentBox();
			addChild(register_content_box);
			register_content_box.visible = false;

			// Apply the alignment to the search content box
			var RegisterContentBoxParem = {x:0,y:0,offsetX:175,offsetY:200};
			new FluidObject(register_content_box,RegisterContentBoxParem);

			/**************************************************************************************/
			_nerdsPageModel = new NerdsPageModel();
			_nerdsPageModel.load(new URLRequest("http://localhost/Jobs4Nerds/XML/list_users.xml"));
			
			_projectsPageModel = new ProjectsPageModel();
			_projectsPageModel.load(new URLRequest("http://localhost/Jobs4Nerds/XML/list_projects.xml"));

			_nerdsPageView = new NerdsPageView(nerds_content_box);
			_nerdsPageView.model = _nerdsPageModel;

			_registerView = new RegisterView(register_content_box);
			_uploadFile = new UploadFile();
			_registerView.model = _uploadFile;
			
			_projectsPageView = new ProjectsPageView(projects_content_box);
			_projectsPageView.model = _projectsPageModel;

			//_controller = new RegisterController(_nerdsPageModel,_registerView);
			//_controller.submitAvatarButton(submit_btn);

			// Add the default content box to the stage
			currentPage = "Home";
			addPageContent(currentPage);
		}

		function addPageContent(page):void
		{
			switch (page)
			{
				case "Home" :
					home_content_box.visible = true;
					projects_content_box.visible = false;
					nerds_content_box.visible = false;
					about_us_content_box.visible = false;
					search_content_box.visible = false;
					register_content_box.visible = false;
					currentPage = "Home";
					break;
				case "Projects" :
					home_content_box.visible = false;
					projects_content_box.visible = true;
					nerds_content_box.visible = false;
					about_us_content_box.visible = false;
					search_content_box.visible = false;
					register_content_box.visible = false;
					currentPage = "Projects";
					break;
				case "Nerds" :
					home_content_box.visible = false;
					projects_content_box.visible = false;
					nerds_content_box.visible = true;
					about_us_content_box.visible = false;
					search_content_box.visible = false;
					register_content_box.visible = false;
					currentPage = "Nerds";
					break;
				case "About Us" :
					home_content_box.visible = false;
					projects_content_box.visible = false;
					nerds_content_box.visible = false;
					about_us_content_box.visible = true;
					search_content_box.visible = false;
					register_content_box.visible = false;
					currentPage = "About Us";
					break;
				case "SearchIcon" :
					home_content_box.visible = false;
					projects_content_box.visible = false;
					nerds_content_box.visible = false;
					about_us_content_box.visible = false;
					search_content_box.visible = true;
					register_content_box.visible = false;
					search_content_box.content_txt.text = "Searching....";
					break;
				case "Register" :
					home_content_box.visible = false;
					projects_content_box.visible = false;
					nerds_content_box.visible = false;
					about_us_content_box.visible = false;
					search_content_box.visible = false;
					register_content_box.visible = true;
					currentPage = "Register";
					break;
			}
		}

		function onClick(e:MouseEvent):void
		{
			switch (e.currentTarget.name)
			{
				case "Home" :
					addPageContent(e.currentTarget.name);
					break;
				case "Projects" :
					addPageContent(e.currentTarget.name);					
					
					_projectsController = new ProjectsController(_projectsPageModel, _projectsPageView);
					_projectsController.addNextProjectButton(projects_content_box.arrow_right_btn);
					_projectsController.addPreviousProjectButton(projects_content_box.arrow_left_btn);
					_projectsController.addDownloadButton(projects_content_box.download_btn);
					
					break;
				case "Nerds" :
					addPageContent(e.currentTarget.name);
					
					_nerdsController = new NerdsController(_nerdsPageModel, _nerdsPageView);
					_nerdsController.addNerdOneButton(nerds_content_box.nerdOne);
					_nerdsController.addNerdTwoButton(nerds_content_box.nerdTwo);
					_nerdsController.addNerdThreeButton(nerds_content_box.nerdThree);
					_nerdsController.addNerdFourButton(nerds_content_box.nerdFour);
					_nerdsController.addNerdFiveButton(nerds_content_box.nerdFive);
					_nerdsController.addNerdSixButton(nerds_content_box.nerdSix);
					
					break;
				case "About Us" :
					addPageContent(e.currentTarget.name);
					break;
				case "SearchIcon" :
					addPageContent(e.currentTarget.name);
					break;
				case "Register" :
					addPageContent(e.currentTarget.name);

					_controller = new RegisterController(_uploadFile,_registerView);
					_controller.submitAvatarButton(register_content_box.browse_files_btn);
					_controller.submitRegisterButton(register_content_box.submit_btn);

					break;
			}
		}

		function doRollOver(e:MouseEvent):void
		{
			switch (e.currentTarget.name)
			{
				case "Home" :
					e.currentTarget.nav_name.textColor = 0x00FF00;
					break;
				case "Projects" :
					e.currentTarget.nav_name.textColor = 0x00FF00;
					break;
				case "Nerds" :
					e.currentTarget.nav_name.textColor = 0x00FF00;
					break;
				case "About Us" :
					e.currentTarget.nav_name.textColor = 0x00FF00;
					break;
				case "Register" :
					e.currentTarget.nav_name.textColor = 0x00FF00;
					break;
			}
		}

		function doRollOut(e:MouseEvent):void
		{
			switch (e.currentTarget.name)
			{
				case "Home" :
					e.currentTarget.nav_name.textColor = 0x333333;
					break;
				case "Projects" :
					e.currentTarget.nav_name.textColor = 0x333333;
					break;
				case "Nerds" :
					e.currentTarget.nav_name.textColor = 0x333333;
					break;
				case "About Us" :
					e.currentTarget.nav_name.textColor = 0x333333;
					break;
				case "Register" :
					e.currentTarget.nav_name.textColor = 0x333333;
					break;
			}
		}
	}
}